﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Liang.Util.Config
{
    /// <summary>
    /// 对app.config文件进行读写操作类
    /// </summary>
    public class AppConfig
    {
        /// <summary>
        /// 写操作
        /// </summary>
        /// <param name="strExecutablePath"></param>
        /// <param name="AppKey"></param>
        /// <param name="AppValue"></param>
        public static void ConfigSetValue(string strExecutablePath, string AppKey, string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            //获取可执行文件的路径和名称
            xDoc.Load(strExecutablePath + ".config");
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//connectionStrings");
            // xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@name='" + AppKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("connectionString", AppValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("name", AppKey);
                xElem2.SetAttribute("connectionString", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(strExecutablePath + ".config");
        }

        /// <summary>
        /// 读操作
        /// </summary>
        /// <param name="strExecutablePath"></param>
        /// <param name="appKey"></param>
        /// <returns></returns>
        public static string ConfigGetValue(string strExecutablePath, string appKey)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(strExecutablePath + ".config");
                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode("//connectionStrings");
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@name='" + appKey + "']");
                if (xElem != null)
                    return xElem.GetAttribute("connectionString");
                else
                    return "";
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
